# Run regressions for foot traffic analysis

# Setup ----
options(scipen = 999)
library(caret)
library(tidyverse)
library(lfe)
library(stargazer)
library(xtable)




load("/02_Data/FL_visitor_1804_1910_tract.RData") # NOTE: Proprietary data, values of all variables have been replaced with * 
load("/02_Data/tract_flood_FL.RData")             # NOTE: Proprietary data, values of all variables have been replaced with * 
load("/02_Data/NFIP_Michael_Claim.RData")
load("/02_Data/tract2017.RData")
load("/02_Data/NFIPTakeup.RData")
load("/02_Data/FloodZone.RData")
load("/02_Data/SavingsAccount.RData")
load("/02_Data/SCI_v3.RData")





# tract-level data as of 2017 --------------------------------------------------
tract_data = tract2017 %>%
  left_join(tract_flood_FL, by=c("GEOID"="FIPS_tract")) %>%
  left_join(NFIPTakeup, by="GEOID") %>%
  left_join(FloodZone, by="GEOID") %>%
  left_join(SavingsAccount, by="GEOID") %>%
  left_join(SCI_v3, by=c("GEOID"="tract")) %>%
  mutate(FloodDepth_ft_tract_dummy = ifelse(FloodDepth_ft_tract>0, 1, 0))



# tract-business-time level data, April 2018 to October 2019 -------------------
FL_visitor_1804_1910 = FL_visitor_1804_1910_tract %>%
  left_join(NFIP_Michael_Claim, by=c("visitor_home_tract"="CensusTract", "month")) %>%
  left_join(tract_data, by=c("visitor_home_tract"="GEOID")) %>%
  mutate(
    FloodDepth_ft_tract_dummy = ifelse(FloodDepth_ft_tract>0, 1, 0),
    ClaimAmount               = ifelse(is.na(ClaimAmount), 0, ClaimAmount),
    WAvgPaidWeek              = ifelse(is.na(WAvgPaidWeek), 0, WAvgPaidWeek),
    month                     = factor(month)
  )

date.binarized = predict(dummyVars(formula = "~ month", FL_visitor_1804_1910, sep = '_'), FL_visitor_1804_1910)        # the "dummyVars" formula in the "caret" package can be used to binarize the categotical variable automatically
colnames(date.binarized) = substr(colnames(date.binarized)[1:length(colnames(date.binarized))], 7, nchar(colnames(date.binarized)[1:length(colnames(date.binarized))]))

FL_visitor_1804_1910 = cbind.data.frame(FL_visitor_1804_1910, date.binarized)
rm(date.binarized)










################################################################################
# Table 4: Effect of Flood Insurance Claim Payouts and Payout Timing
################################################################################

b1 = felm(log(1+visitor_count_home_tract) ~ (FloodDepth_ft_tract + FloodDepth_ft_tract:log(1+ClaimAmount) +
                                               
                                             FloodDepth_ft_location + NAICS_2digit_name + dummy_brands +
                                             log_medincome2017 + log_population2017 + ptg_Unemployed2017 +
                                             ptg_white2017 + ptg_OwnerOccupied2017 + ptg_Mortgage2017 + ptg_Bachelor2017 + Gini2017 + 
                                             ptg_SavingsAccount2017 + FloodZone_Share_Dev_Area_tract):Post | 
            
            factor(visitor_home_tract_placekey) + factor(month) | 0 | visitor_home_tract, data = FL_visitor_1804_1910)

b2 = felm(log(1+visitor_count_home_tract) ~ (FloodDepth_ft_tract + FloodDepth_ft_tract:log(1+ClaimAmount) + 
                                             FloodDepth_ft_tract:log(1+ClaimAmount):WAvgPaidWeek + FloodDepth_ft_tract:log(1+ClaimAmount):I(WAvgPaidWeek*WAvgPaidWeek) +
                                               
                                             FloodDepth_ft_location + NAICS_2digit_name + dummy_brands +
                                             log_medincome2017 + log_population2017 + ptg_Unemployed2017 +
                                             ptg_white2017 + ptg_OwnerOccupied2017 + ptg_Mortgage2017 + ptg_Bachelor2017 + Gini2017 + 
                                             ptg_SavingsAccount2017 + FloodZone_Share_Dev_Area_tract):Post | 
            
            factor(visitor_home_tract_placekey) + factor(month) | 0 | visitor_home_tract, data = FL_visitor_1804_1910)


b3 = felm(log(1+visitor_count_home_tract) ~ (FloodDepth_ft_tract + FloodDepth_ft_tract:FloodInsTakeup + FloodDepth_ft_tract:log(1+ClaimAmount) +
                                               
                                             FloodDepth_ft_location + NAICS_2digit_name + dummy_brands +
                                             log_medincome2017 + log_population2017 + ptg_Unemployed2017 +
                                             ptg_white2017 + ptg_OwnerOccupied2017 + ptg_Mortgage2017 + ptg_Bachelor2017 + Gini2017 + 
                                             ptg_SavingsAccount2017 + FloodZone_Share_Dev_Area_tract):Post | 
            
            factor(visitor_home_tract_placekey) + factor(month) | 0 | visitor_home_tract, data = FL_visitor_1804_1910)

b4 = felm(log(1+visitor_count_home_tract) ~ (FloodDepth_ft_tract + FloodDepth_ft_tract:FloodInsTakeup + FloodDepth_ft_tract:log(1+ClaimAmount) + 
                                             FloodDepth_ft_tract:log(1+ClaimAmount):WAvgPaidWeek + FloodDepth_ft_tract:log(1+ClaimAmount):I(WAvgPaidWeek*WAvgPaidWeek) +
                                               
                                             FloodDepth_ft_location + NAICS_2digit_name + dummy_brands +
                                             log_medincome2017 + log_population2017 + ptg_Unemployed2017 +
                                             ptg_white2017 + ptg_OwnerOccupied2017 + ptg_Mortgage2017 + ptg_Bachelor2017 + Gini2017 + 
                                             ptg_SavingsAccount2017 + FloodZone_Share_Dev_Area_tract):Post | 
            
            factor(visitor_home_tract_placekey) + factor(month) | 0 | visitor_home_tract, data = FL_visitor_1804_1910)


stargazer(b1, b2, b3, b4,
          
          type = 'text',
          #         type = 'latex',
          omit = c("NAICS", "location", "2017", "brands", "Share"),
          omit.stat = c('ser'),
          omit.table.layout = 'n',
          
          float = F,
          column.sep.width = "2pt",
          notes.append = F,
          font.size="small"
          
)



